Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

186
Views
When adding 1 to the index, at the point where I > array.length, undefined is returned. What am I doing wrong?

Currently, im adding 1 to the index on click, to simulate a user controlled iteration of the array. I have a conditional statement where if i > length, i = 0; Yet im still getting undefined after reaching the end of the array.

    const marqueeMonthlies = () => {
        intervalId = setInterval(() => {
            insertMarquee.empty('');
            // i %= marqueeMonthlyPlates.length
            insertMarquee.append(
                `<div class="container" id="generatedMonthlyPlates">
                <input type="text" width="100px" name="monthlyLicensePlate" id="${marqueeMonthlyPlates[i]}" class="licensePlate" placeholder="AB12345" value="${marqueeMonthlyPlates[i]}"/>
                </div>`
            )
            // i++;     
        }, 5000);
    }

    const getNextMonthly = () => {
        if ( i < marqueeMonthlyPlates.length) {
            i = i+1
            return marqueeMonthlyPlates[i]    
        } else
        if (i > marqueeMonthlyPlates.length){
            i = 0;
            return marqueeMonthlyPlates[i];
        } else 
        if (marqueeMonthlyPlates.length === undefined ){
            i = 0;
            return marqueeMonthlyPlates[i];
        }
        
    }

I tried to catch for when undefined present, to set the index back to 0, but it only corrects itself when 1 is added to i again on another click. What am I doing wrong here?

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

The answer was to change my greater than statement, as the goal was to bring i back to 0 when it reached the end.

        if (i > marqueeMonthlyPlates.length){
            i = 0;
            return marqueeMonthlyPlates[i];

to

        if (i >= marqueeMonthlyPlates.length){
            i = 0;
            return marqueeMonthlyPlates[i];
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!